<?php
echo 1;die;//测试下git
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');

//$parentPath = $argv[1];
$parentPath = './';

require_once $parentPath . 'model/meinianAnalysis.php';
require_once $parentPath . 'model/cimingAnalysis.php';
require_once $parentPath . 'model/aikangAnalysis.php';
require_once $parentPath . 'model/reportStandard.php';
require_once $parentPath . 'model/reportManage.php';

$logStr = '';

function json_encode_ex($value) {
    if (version_compare(PHP_VERSION, '5.4.0', '<')) {
        $str = json_encode($value);
        $str = preg_replace_callback(
                "#\\\u([0-9a-f]{4})#i", function($matchs) {
            return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
        }, $str
        );
        return $str;
    } else {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }
}

function file_isEmpty($backup_path, $path, $parentPath) {
    $i = 0;
    if ($handle = @opendir($backup_path)) {
        $file_array = array();
        while (false !== ($file = readdir($handle))) {//读取文件夹里的文件
            if ($file != "." && $file != "..") {
                $file_array[$i]["filename"] = $file;
                $i++;
            } else {
                $file_array = $file_array;
            }
        }
        if (!empty($file_array)) {
            foreach ($file_array as $key => $value) {
                $key = checkExits($path, $value['filename']);
                if ($key == FALSE) {
                    if ($backup_path == $parentPath . 'ftp/meinian/') {
                        $data_json = meinianParse($backup_path . $value['filename']);
                    } elseif ($backup_path == $parentPath . 'ftp/ciming/') {
                        $data_json = cimingParase($backup_path . $value['filename']);
                    } else {
                        $data_json = aikangParseDan($backup_path . $value['filename']);
                    }
                    $report_dir = $path;
                    if ($backup_path == $parentPath . 'ftp/aikang_txt/') {
                        $report_name = basename($value['filename'], ".txt") . '.txt';
                        file_put_contents($report_dir . $report_name, $data_json);
                        rm_jiexi_baocun($backup_path, $value['filename'], $report_dir, $report_name);
                    } else {
                        $report_name = basename($value['filename'], ".html") . '.txt';
                        file_put_contents($report_dir . $report_name, $data_json);
                        rm_jiexi_baocun($backup_path, $value['filename'], $report_dir, $report_name);
                    }
                }
            }
        } else {
            // echo '******No new report.*******';
            //exit(1);
            return 1;
        }
        closedir($handle); //关闭文件夹
    } else {
        //echo '******Fail to open file.******';
        //exit(2);
        return 2;
    }
    return 0;
}

/*
 * 检测报告是否被解析了
 */

function checkExits($path, $reportName) {
    if ($handle = @opendir($path)) {
        $file_array = array();
        while (false !== ($file = readdir($handle))) {//读取文件夹里的文件
            if ($file != "." && $file != "..") {
                $file_array[] = basename($file, ".txt");
            } else {
                $file_array = $file_array;
            }
        }
    }
    $key = array_search(basename($reportName, ".pdf"), $file_array);
    if ($key == FALSE) {
        $key = array_search(basename($reportName, ".html"), $file_array);
    }
    return $key;
}

function rm_jiexi_baocun($backup_path, $file_name, $report_dir, $report_name) {
    //如果解析成功，将报告在原文件夹中删除，并保存到对应的文件夹中，将解析结果保存到对应的解析文件夹中；解析失败，不删，不解析，也不存，
    if (strstr($backup_path, 'meinian')) {
        /**
         * 设置美年体检报告，以及报告解析文件的保存路径
         */
        $table = 'meinian';
        @rename($backup_path . $file_name, "./ftp/meinian_bak/" . $file_name);
    } elseif (strstr($backup_path, 'ciming')) {
        /**
         * 设置慈铭体检报告，以及报告解析文件的保存路径
         */
        $table = 'ciming';
        @rename($backup_path . $file_name, "./ftp/ciming_bak/" . $file_name);
    } else {
        /**
         * 设置爱康体检报告，以及报告解析文件的保存路径
         */
        $table = 'aikang';
        @rename("./ftp/aikang/" . basename($file_name, ".txt") . ".pdf", "./ftp/aikang_bak/" . basename($file_name, ".txt") . ".pdf");
        //@unlink($backup_path . $file_name);
    }

    /*
     * 调用标准化
     */
    $reportStandard = new reportStandard();
    $arrayItemResult = json_decode(file_get_contents($report_dir . $report_name));
    $arrayItemResult->data = $reportStandard->standard($arrayItemResult->data, $table, $dbInsert = false);
    $arrayItemResult = json_encode_ex($arrayItemResult);
    file_put_contents($report_dir . $report_name, $arrayItemResult);
}

$path = $parentPath . "ftp/json/";
$meinianResult = file_isEmpty($parentPath . "ftp/meinian/", $path, $parentPath);
if ($meinianResult == 1) {
    echo '****** Meinian No new report.*******' . "<br/>";
} else if ($meinianResult == 2) {
    echo '****** Meinian Fail to open file.******' . "<br/>";
}
$cimingResult = file_isEmpty($parentPath . "ftp/ciming/", $path, $parentPath);
if ($cimingResult == 1) {
    echo '****** Ciming No new report.*******' . "<br/>";
} else if ($cimingResult == 2) {
    echo '****** Ciming Fail to open file.******' . "<br/>";
}
$aikangResult = file_isEmpty($parentPath . "ftp/aikang_txt/", $path, $parentPath);
if ($aikangResult == 1) {
    echo '****** Aikang No new report.*******' . "<br/>";
} else if ($aikangResult == 2) {
    echo '****** Aikang Fail to open file.******' . "<br/>";
}
if ($meinianResult == 0 || $cimingResult == 0 || $aikangResult == 0) {
    exit(0);
} else {
    exit(1);
}
?>
